import 'package:flutter/material.dart';
import 'package:regular_assess/helper/global/user/logic.dart';
import 'package:regular_assess/helper/utils/httputils/http_man.dart';
import 'package:regular_assess/helper/utils/httputils/urlpath.dart';

import '../../../helper/utils/util.dart';
import 'index.dart';

class LoginController extends GetxController {
  LoginController();

  final state = LoginState();

  void actionLogin() {
    if (state.accountCtrl.text.isEmpty) {
      Hud.toast('请输入账号');
      state.accountError = '请输入账号';
      update();
      return;
    }
    if (state.pwdCtrl.text.isEmpty) {
      var placeholder = state.isPwdLogin ? '请输入密码' : '请输入验证码';
      Hud.toast(placeholder);
      state.pwdError = placeholder;
      update();
      return;
    }
    login(state.accountCtrl.text, state.pwdCtrl.text);
  }

  void login(String account, String pwd) async {
    var res = await HttpMan.instance
        .post(UserController.instance.urlOrigin + UrlPath.login, args: {
      "appCode": "jx-clivil-servant-assessment",
      "loginName": account,
      "loginPassword": pwd,
      "loginType": 1,
      "platType": 1,
      "resolution": "${Get.height}*${Get.width}",
      "screenSize": "${Get.height}*${Get.width}",
      "userOs": GetPlatform.isAndroid ? "Andriod" : "IOS",
    });
    if (res.isSuccess) {
      await UserController.instance.loginSuccess(res.data);
      UserController.instance.toWeb();
    } else {
      Hud.toast(res.msg);
    }
  }

  void toForgetPwd() {
    Get.toNamed(RouterPath.forgot_new_pwd);
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();

    state.accountCtrl = TextEditingController();
    state.pwdCtrl = TextEditingController();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
